package com.app.aipass.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.databinding.ActivitySplashBinding;
import com.app.aipass.entity.Message;
import com.app.aipass.utils.RxTimerUtil;
import com.app.aipass.utils.ShareUtil;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.RxBus;

/**
 * @author: woking
 * @date: 2021/7/1
 * @description：
 */
public class SplashActivity extends BaseActivity<ActivitySplashBinding, BaseViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_splash;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
//        RxTimerUtil.timer(1800, number -> {
//            if(!isGetToken)
//            {
//                Bundle bundle = new Bundle();
//                bundle.putBoolean("isGetToken",isGetToken);
//                startActivity(MainActivity.class,bundle);
//            }
//
//            finish();
//        });

        RxTimerUtil.timer(1800, number -> {
            String token = ShareUtil.getInstance().getString(ShareUtil.ACCESS_TOKEN,"");
            if (token.isEmpty()){
                startActivity(LoginActivity.class);
            }else {
                startActivity(MainActivity.class);
            }
            finish();
        });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        statusBarTransparent();
//        Glide.with(this).load(R.drawable.splash_bg).override(540, 1000).into(binding.splashImg);
//        Glide.with(this).load(R.drawable.splash_bg).downloadOnly(new SimpleTarget() {
//            @Override
//            public void onResourceReady(@NonNull Object resource, @Nullable Transition transition) {
//                Uri uri = Uri.fromFile((File) resource);
//                binding.splashImg.setImageURI(uri);
//            }
//        });
        Log.e("TAG", "=====123123====onCreate========1111====token========"+getIntent().getStringExtra("token"));
        getToken(getIntent());

        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (!this.isTaskRoot()) { // 判断当前activity是不是所在任务栈的根
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("TAG", "=====123123====onNewIntent========1111====token========"+intent.getStringExtra("token"));
        setIntent(intent);
        getToken(intent);
    }

    private boolean isGetToken = false;
    private void getToken(Intent intent) {
        if(intent != null)
        {
            Log.e("TAG", "=====123123====getToken=====2222=======token========"+intent.getStringExtra("token"));
            if(intent.hasExtra("token"))
            {
                Log.e("TAG", "=====123123====getToken=====3333=======token========"+intent.getStringExtra("token"));
                ShareUtil.getInstance().save(ShareUtil.ACCESS_TOKEN,intent.getStringExtra("token"));
                RxBus.getDefault().post(new Message("start_login",""));
                isGetToken = true;
            }
        }
    }
}